//
//  CZNetworkTool.swift
//  iOSWeibo10
//
//  Created by itcast on 16/5/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking

// 请求方法枚举
enum RequestMethod: String {
    case GET = "GET"
    case POST = "POST"
}


// 继承NSObject,不让别人直接调用AFN的GET和POST方法
class CZNetworkTool: NSObject {
    // 单例
    static let sharedInstance: CZNetworkTool = CZNetworkTool()
    
    // 属性AFHttpSessionManager  
    // https://api.weibo.com/oauth2/access_token
    // https://api.weibo.com/2/statuses/home_timeline.json
    // 常量: 后面的代码块可以多做一些操作
    private let afnManager: AFHTTPSessionManager = {
        let afn = AFHTTPSessionManager(baseURL: NSURL(string: "https://api.weibo.com/"))
        // 给响应的序列化器多添加支持的类型
        afn.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return afn
    }()
    
//    func requestGET(URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {
//        afnManager.GET(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
//    }
//
//    func requestPOST(URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {
//        afnManager.POST(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
//    }
    
    // 如果直接使用AFN的GET和POST方法,如果AFN改变了方法名或参数改变了,哪项目中直接使用AFN.GET和POST的地方都需要改变
    // 自己封装GET 和 POST方法,项目中要使用GET和POST就用我们封装好的,到时如果如果AFN改变了方法名或参数改变了,只需要改我们封装的方法
    /// 封装AFN的GET和POST,通过参数来确定使用GET还是POST
    func request(method: RequestMethod, URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {
        // GET和POST需要的参数我们这个方法里面不能确定,只有调用的人传递进来
        if method == RequestMethod.GET {
            afnManager.GET(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        } else {
            afnManager.POST(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
    }
    
    /**
     AFN POST 上传文件
     
     - parameter URLString:                 <#URLString description#>
     - parameter parameters:                <#parameters description#>
     - parameter constructingBodyWithBlock: <#constructingBodyWithBlock description#>
     - parameter success:                   <#success description#>
     - parameter failure:                   <#failure description#>
     */
    func POST(URLString: String, parameters: AnyObject?, constructingBodyWithBlock: ((AFMultipartFormData) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {
        afnManager.POST(URLString, parameters: parameters, constructingBodyWithBlock: constructingBodyWithBlock, progress: nil, success: success, failure: failure)
    }
}
